package DAO;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Beans.OrdenCompraBean;
import Beans.SucursalBean;
import HibernateUtils.HibernateUtil;

public class HibernateOrdenCompraDAO {
	private static HibernateOrdenCompraDAO instancia;
	private static SessionFactory sf;
	private HibernateOrdenCompraDAO(){
		
	}
	
	public static HibernateOrdenCompraDAO getInstancia(){
		if(instancia==null){
			sf=HibernateUtil.getSessionFactory();
			instancia=new HibernateOrdenCompraDAO();
		}
		return instancia;
	}
	
	public void grabarOrdenCompra(OrdenCompraBean fb){
		Session session=sf.openSession();
		session.beginTransaction();
		session.save(fb);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public OrdenCompraBean buscarOrdenCompra(OrdenCompraBean fb) {
		Session session=sf.openSession();
		OrdenCompraBean f=(OrdenCompraBean) session.get(OrdenCompraBean.class, fb.getId());
		session.close();
		return f;
	}
	
	public List<OrdenCompraBean> list() {
		Session sesion=sf.openSession();
		sesion.beginTransaction();
		List<OrdenCompraBean> ordenes=sesion.createQuery("from OrdenCompraBean").list();
		return ordenes;
	}
}
